package my.lotu.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;


import my.lotu.bean.CollectData;
import my.lotu.bean.Daymeal;
import my.lotu.bean.Food;
import my.lotu.bean.Meal;
import my.lotu.bean.Plan;
import my.lotu.service.IPlanService;

@SuppressWarnings("serial")
public class GetPlanAction extends ActionSupport{
	private Plan plan;
	private IPlanService planService;
	private CollectData collectData;
	private static List<Daymeal> listDaymeal=null;
	private List<Meal> listMeals;

	private List<Food> listFoodBreakfast;
	private List<Food> listFoodLunch;
	private List<Food> listFoodDinner;
	@Override
	public String execute(){
		if(plan==null){
			return ERROR;
		}
		listDaymeal=planService.getDayMealByPlan(plan);
		getInfoByDay(0);
		return SUCCESS;
	}
	private void getInfoByDay(int id){
		collectData=new CollectData();
		collectData.setMoney(plan.getCost());
		collectData.setDay(plan.getDay());
		collectData.setDayId(id+1);
		listMeals=planService.getMealByDaymeal(getListDaymeal().get(id));
		listFoodBreakfast=planService.getFoodByMeal(listMeals.get(0));
		listFoodLunch=planService.getFoodByMeal(listMeals.get(1));
		listFoodDinner=planService.getFoodByMeal(listMeals.get(2));
		int cost=3;
		for(int i=getListDaymeal().size()-1;i>=0;i--){
			cost+=getListDaymeal().get(i).getCost();
		}
		collectData.setCost(cost);
	}
	public String getDay(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int dayId=Integer.parseInt(request.getParameter("dayId"));
		getInfoByDay(dayId);
		return SUCCESS;
	}
	public IPlanService getPlanService() {
		return planService;
	}

	public void setPlanService(IPlanService planService) {
		this.planService = planService;
	}

	public Plan getPlan(){
		return plan;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}
	public CollectData getCollectData() {
		return collectData;
	}

	public void setCollectData(CollectData collectData) {
		this.collectData = collectData;
	}
	
	
	public List<Food> getListFoodLunch() {
		List<Food> list=listFoodLunch.subList(0, listFoodLunch.size());
		return list;
	}
	public void setListFoodLunch(List<Food> listFoodLunch) {
		this.listFoodLunch = listFoodLunch;
	}
	public List<Food> getListFoodDinner() {
		List<Food> list=listFoodDinner.subList(0, listFoodDinner.size());
		return list;
	}
	public void setListFoodDinner(List<Food> listFoodDinner) {
		this.listFoodDinner = listFoodDinner;
	}
	public List<Food> getListFoodBreakfast() {
		List<Food> list=listFoodBreakfast.subList(0, listFoodBreakfast.size());
		return list;
	}
	public void setListFoodBreakfast(List<Food> listFoodBreakfast) {
		this.listFoodBreakfast = listFoodBreakfast;
	}
	
	
	public List<Daymeal> getListDaymeal() {
		
		return listDaymeal;
	}

	public List<Meal> getListMeals() {
		return listMeals;
	}
	public void setListMeals(List<Meal> listMeals) {
		this.listMeals = listMeals;
	}

}
